import {px2rpx} from "../../../../utils/tool";
import {getLimitGoods} from "../../../../service/goods";

Component({
    properties: {
        cards:{type:Array,value:[]},
        marginBottom:{
            type:Number, value:0,
            observer(data){
                if (data){
                    this.setData({
                        _marginBottom:px2rpx(data),
                    })
                }
            }
        },
        marginX: {
            type:Number, value:0,
            observer(data){
                if (data){
                    this.setData({
                        _marginX:px2rpx(data),
                    })
                }

            }
        },
        radius:{type:String, value:'fillet'},
        colNum:{type:String, value:'col_2'},
        background:{type:String, value:'none'},
        tpl:{type:String, value:'tpl_1'},
        everyCardNum:{type:Number, value:6}    // 每张卡片显示的商品数量
    },
    data: {
        activeIndex:0,
    },
    lifetimes:{
        attached() {
            (async () => {
                for(let i in this.data.cards){
                    this.setGoods(i)
                }
            })()
        }
    },
    methods: {
        setGoods(index){
            const {cards, everyCardNum } = this.data
            const card = cards[index]
            const where = {
                limit: everyCardNum,
            }
            if(card.source_type == 'goods_cate'){
                where.cate_id = card.source_id
            }else if(card.source_type == 'goods_tag'){
                where.tag_id = card.source_id
            }
            getLimitGoods(where).then((res) => {
                const {items} = res
                this.setData({
                    [`cards[${index}].list`]:items
                })
            })
        },

        cardChange(e){
            const {index} = e.currentTarget.dataset
            this.setData({
                activeIndex: index,
            })
        },
    }
});
